<?php

/**
 * Created by PhpStorm.
 * User: shwy
 * Date: 2017/9/8
 * Time: 9:40
 * 采购管理相关接口
 */
class procurement extends CI_Controller
{


    public function __construct()
    {
        parent::__construct();
        $this->load->model('procurement_model');
        $this->load->model('purchaseOrder_model');
        $this->load->model('stockPut_model');
        $this->load->model('stockOut_model');

        $this->load->model('returnOrder_model');
        $this->load->model('check_model');
        $this->load->model('goodsStockPrice_model');
        $this->load->model('project_model');
        $this->load->model('supplier_model');

        $this->load->model('biddingconfirm_model');

    }


    //采购需求单的筛选搜索接口
    public function search()
    {

        $argc = [];
        if (isset($_GET['startTime']) && $_GET['startTime']) {
            $argc += [
                'createTime >' => strtotime($_GET['startTime']),
            ];
        }

        if (isset($_GET['endTime']) && $_GET['endTime']) {
            $argc += [
                'createTime <' => strtotime($_GET['endTime']),
            ];
        }

        if (isset($_GET['status']) && $_GET['status'] != '' && $_GET['status'] != 'null') {
            $argc += [
                'status ' => $_GET['status']
            ];

        }

        if (isset($_GET['search']) && $_GET['search'] != '') {
            $searchArg = [
                'field' => 'name',
                'arg' => $_GET['search'],
                'side' => 'both'
            ];
        } else {
            $searchArg = '';
        }


        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if (!$page) {
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        } else {
            $start_position = ($page - 1) * $page_num;
        }

        if (!$argc) {
            $argc = [];
        }

        $argc += [
            'type !=' => '采购项目单'
        ];

        if ($searchArg) {
            $data = $this->procurement_model->search('*', $argc, $searchArg, $page_num, $start_position);
        } else {
            $data = $this->procurement_model->page('procurement', $argc, $page_num, $start_position); //limit查询的数据
        }

        foreach ($data[0] as $k=>$v){
            unset($data[0][$k]['goodsMessage']);
        }




        $data['pageNum'] = $page_num;
        $data['this_page'] = $page;


        echo json_encode($data, JSON_UNESCAPED_UNICODE);

    }

    //采购进货单的筛选搜索接口
    public function purchaseOrderSearch()
    {

        $argc = [];
        if (isset($_GET['startTime']) && $_GET['startTime']) {
            $argc += [
                'createTime >' => strtotime($_GET['startTime']),
            ];
        }

        if (isset($_GET['endTime']) && $_GET['endTime']) {
            $argc += [
                'createTime <' => strtotime($_GET['endTime']),
            ];
        }

        if (isset($_GET['status']) && $_GET['status'] != '' && $_GET['status'] != 'null') {
            $argc += [
                'status ' => $_GET['status']
            ];
        }

        if (isset($_GET['supplier']) && $_GET['supplier'] && $_GET['supplier'] != 'null') {
            $argc += [
                'supplier' => $_GET['supplier'],
            ];
        }

        if (isset($_GET['search']) && $_GET['search'] != '' && $_GET['search'] != 'null') {
            $searchArg = [
                'field' => 'name',
                'arg' => $_GET['search'],
                'side' => 'both'
            ];
        } else {
            $searchArg = '';
        }


        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if (!$page) {
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        } else {
            $start_position = ($page - 1) * $page_num;
        }

        if (!$argc) {
            $argc = [];
        }



        if ($searchArg) {

            $data = $this->purchaseOrder_model->search('*', $argc, $searchArg, $page_num, $start_position);
        } else {
            $data = $this->purchaseOrder_model->page('purchaseOrder', $argc, $page_num, $start_position); 
        }

        foreach ($data[0] as $k=>$v){
            unset($data[0][$k]['goodsMessage']);
        }

        $data['pageNum'] = $page_num;
        $data['this_page'] = $page;

        echo json_encode($data, JSON_UNESCAPED_UNICODE);

    }


    //采购项目的筛选搜索接口
    public function proProjectSearch()
    {

        $argc = [];
        if (isset($_GET['startTime']) && $_GET['startTime']) {
            $argc += [
                'createTime >' => strtotime($_GET['startTime']),
            ];
        }

        if (isset($_GET['endTime']) && $_GET['endTime']) {
            $argc += [
                'createTime <' => strtotime($_GET['endTime']),
            ];
        }

        if (isset($_GET['status']) && $_GET['status'] != '' && $_GET['status'] != 'null') {
            $argc += [
                'status ' => $_GET['status']
            ];

        }

        if (isset($_GET['search']) && $_GET['search'] != '') {
            $searchArg = [
                'field' => 'name',
                'arg' => $_GET['search'],
                'side' => 'both'
            ];
        } else {
            $searchArg = '';
        }


        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if (!$page) {
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        } else {
            $start_position = ($page - 1) * $page_num;
        }

        if (!$argc) {
            $argc = [];
        }

        $argc += [
            'type =' => '采购项目单'
        ];

        if ($searchArg) {
            $data = $this->procurement_model->search('*', $argc, $searchArg, $page_num, $start_position);
        } else {
            $data = $this->procurement_model->page('procurement', $argc, $page_num, $start_position); //limit查询的数据
        }

        foreach ($data[0] as $k=>$v){
            unset($data[0][$k]['goodsMessage']);
        }




        $data['pageNum'] = $page_num;
        $data['this_page'] = $page;


        echo json_encode($data, JSON_UNESCAPED_UNICODE);

    }

    //采购退货的筛选搜索接口
    public function returnOrderSearch()
    {

        $argc = [];
        if (isset($_GET['startTime']) && $_GET['startTime']) {
            $argc += [
                'createTime >' => strtotime($_GET['startTime']),
            ];
        }

        if (isset($_GET['endTime']) && $_GET['endTime']) {
            $argc += [
                'createTime <' => strtotime($_GET['endTime']),
            ];
        }

        if (isset($_GET['status']) && $_GET['status'] != '' && $_GET['status'] != 'null') {
            $argc += [
                'status ' => $_GET['status']
            ];

        }

        if (isset($_GET['search']) && $_GET['search'] != '') {
            $searchArg = [
                'field' => 'name',
                'arg' => $_GET['search'],
                'side' => 'both'
            ];
        } else {
            $searchArg = '';
        }


        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if (!$page) {
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        } else {
            $start_position = ($page - 1) * $page_num;
        }

        if (!$argc) {
            $argc = [];
        }

        $argc += [
            'type =' => '采购退货单'
        ];

        if ($searchArg) {
            $data = $this->returnOrder_model->search('*', $argc, $searchArg, $page_num, $start_position);
        } else {
            $data = $this->returnOrder_model->page('returnOrder', $argc, $page_num, $start_position); //limit查询的数据
        }


        foreach ($data[0] as $k => $v) {
            if (!$v['isEnd'] && $v['status'] == 2) {
                //退货和换货都叫出库单
                $stockPut = [
                    'type' => $v['type'],
                    'stockOutCode' => 'CK' . date('Ymd') . rand(100, 999),
                    'returnCode' => $v['code'], //关联退货单编号
                    'createMan' => $_COOKIE['nickname'],
                    'supplier' => $v['supplier'],
                    'content' => $v['content'],
                    'status' => 0,
                    'createTime' => time(),
                    'attribute' => $v['attribute'],
                    'goodsMessage' => $v['goodsMessage'],
                    'relevanceId' => $v['id'] //id添加使之关联
                ];

                $this->stockOut_model->add($stockPut);

                $data2 = [
                    'isEnd' => 1
                ];
                $arg2 = [
                    'id' => $v['id']
                ];
                $this->returnOrder_model->edit($data2, $arg2);

            }
        }



        $data['pageNum'] = $page_num;
        $data['this_page'] = $page;


        echo json_encode($data, JSON_UNESCAPED_UNICODE);

    }


    //采购换货的筛选搜索接口
    public function barterOrderSearch()
    {

        $argc = [];
        if (isset($_GET['startTime']) && $_GET['startTime']) {
            $argc += [
                'createTime >' => strtotime($_GET['startTime']),
            ];
        }

        if (isset($_GET['endTime']) && $_GET['endTime']) {
            $argc += [
                'createTime <' => strtotime($_GET['endTime']),
            ];
        }

        if (isset($_GET['status']) && $_GET['status'] != '' && $_GET['status'] != 'null') {
            $argc += [
                'status ' => $_GET['status']
            ];

        }

        if (isset($_GET['search']) && $_GET['search'] != '') {
            $searchArg = [
                'field' => 'name',
                'arg' => $_GET['search'],
                'side' => 'both'
            ];
        } else {
            $searchArg = '';
        }


        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if (!$page) {
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        } else {
            $start_position = ($page - 1) * $page_num;
        }

        if (!$argc) {
            $argc = [];
        }

        $argc += [
            'type =' => '采购换货单'
        ];

        if ($searchArg) {
            $data = $this->returnOrder_model->search('*', $argc, $searchArg, $page_num, $start_position);
        } else {
            $data = $this->returnOrder_model->page('returnOrder', $argc, $page_num, $start_position); //limit查询的数据
        }


        foreach ($data[0] as $k => $v) {
            if (!$v['isEnd'] && $v['status'] == 2) {
                //退货和换货都叫出库单
                $stockPut = [
                    'type' => $v['type'],
                    'stockOutCode' => 'CK' . date('Ymd') . rand(100, 999),
                    'returnCode' => $v['code'], //关联退货单编号
                    'createMan' => $_COOKIE['nickname'],
                    'supplier' => $v['supplier'],
                    'content' => $v['content'],
                    'status' => 0,
                    'createTime' => time(),
                    'attribute' => $v['attribute'],
                    'goodsMessage' => $v['goodsMessage'],
                ];

                $this->stockOut_model->add($stockPut);

                $data2 = [
                    'isEnd' => 1
                ];
                $arg2 = [
                    'id' => $v['id']
                ];
                $this->returnOrder_model->edit($data2, $arg2);

            }
        }


        $data['pageNum'] = $page_num;
        $data['this_page'] = $page;


        echo json_encode($data, JSON_UNESCAPED_UNICODE);

    }






































}










































